package com.twt.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twt.common.exceptions.AppException;
import com.twt.common.utils.Assert;
import com.twt.model.system.entity.SysParamsPo;
import com.twt.model.base.SysConsts;
import com.twt.sys.base.AbstractBaseService;
import com.twt.sys.mapper.SysParamsMapper;
import com.twt.sys.service.SysParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @Desciption: 参数管理
 * @ClassName:SysParamServiceImpl
 * @Author:TwT
 * @Date:2022/6/16 22:25
 * @Version:1.0
 **/
@Service
@DS("sys")
@DSTransactional
public class SysParamServiceImpl extends AbstractBaseService<SysParamsMapper, SysParamsPo> implements SysParamService {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Description: 添加新的参数
     * @Author: TWT
     * @Date: 22:30 2022/6/16
     * @param:[sysParamsPo]
     * @return:void
     **/
    @Override
    public void add(SysParamsPo sysParamsPo) {
        this.beforeSave(sysParamsPo);

        //参数代码不能重复
        this.checkParamCodeUnique(sysParamsPo.getParamCode());

        sysParamsPo.setId(IdUtil.getSnowflakeNextIdStr());
        sysParamsPo.setIsSystem(SysConsts.NO);
        this.save(sysParamsPo);

        redisTemplate.opsForValue().set("test","hello world");
    }

    /**
     * @Description: 编辑
     * @Author: TWT
     * @Date: 22:31 2022/6/16
     * @param:[sysParamsPo]
     * @return:void
     **/
    @Override
    public void editById(SysParamsPo sysParamsPo) {
        Assert.isBlank(sysParamsPo.getId(), "主键不能为空！");
        this.beforeSave(sysParamsPo);

        //查询修改前的
        SysParamsPo old = this.getById(sysParamsPo.getId());
        if (!old.getParamCode().equals(sysParamsPo.getParamCode())) {
            this.checkParamCodeUnique(sysParamsPo.getParamCode());
        }

        this.updateById(sysParamsPo);
    }


    /**
     * @Description: 分页查询
     * @Author: TWT
     * @Date: 22:34 2022/6/16
     * @param:[page, queryWrapper]
     * @return:com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.twt.model.system.entity.SysParamsPo>
     **/
    @Override
    public Page<SysParamsPo> queryPageByQueryVo(Page<SysParamsPo> page, SysParamsPo queryVo) {
        LambdaQueryWrapper<SysParamsPo> queryWrapper = Wrappers.lambdaQuery(queryVo);
        return this.page(page, queryWrapper);
    }

    /**
     * @Description: 参数校验
     * @Author: TWT
     * @Date: 21:52 2022/6/17
     * @param:[sysParamsPo]
     * @return:void
     **/
    protected void beforeSave(SysParamsPo sysParamsPo) {
        Assert.isBlank(sysParamsPo.getParamCode(), "参数代码不能为空！");
        Assert.isBlank(sysParamsPo.getParamValue(), "参数值不能为空！");
        Assert.isBlank(sysParamsPo.getParamName(), "参数名称不能为空！");
    }

    /**
     * @Description: 校验参数代码是否重复
     * @Author: TWT
     * @Date: 21:53 2022/6/17
     * @param:[paramCode]
     * @return:void
     **/
    private void checkParamCodeUnique(String paramCode) {
        QueryWrapper<SysParamsPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("param_code", paramCode);
        int count = this.count(queryWrapper);
        if (count > 0) throw new AppException(String.format("参数代码[%s]已存在！", paramCode));
    }
}
